package com.mybatis.test.subject5;

import com.dao.GradeDao;
import com.dao.SubjectDao;
import com.entity.Grade;
import com.entity.Subject;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SubjectService {

    SqlSessionFactory factory;

    {
        //读取配置文件
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //构建session工厂
        factory = new SqlSessionFactoryBuilder().build(is);

    }


    public SubjectVO getOne(Integer id){
        SqlSession session = factory.openSession();
        SubjectDao subjectDao = session.getMapper(SubjectDao.class);
        GradeDao gradeDao = session.getMapper(GradeDao.class);
        Subject subjectDB = subjectDao.getOne(id);
        System.out.println(subjectDB);
        SubjectVO subjectVO = new SubjectVO();
        subjectVO.setSubjectNo(subjectDB.getSubjectNo());
        subjectVO.setSubjectName(subjectDB.getSubjectName());
        subjectVO.setClassHour(subjectDB.getClassHour());
        //*********自己去查年级对象，并赋值给vo************
        Grade grade = gradeDao.getOne(subjectDB.getGradeID());
        subjectVO.setGrade(grade);
        //*********************
        System.out.println(subjectVO);
        return subjectVO;
    }

}
